package cc.rengu.igas.channel.cups.core.service.trans;

import cc.rengu.igas.channel.cups.common.constant.CupsParamConstant;
import cc.rengu.igas.channel.cups.common.constant.CupsTreeNodeConstant;
import cc.rengu.igas.channel.cups.common.enums.RespCodeEnum;
import cc.rengu.igas.channel.cups.core.service.base.ManageService;
import cc.rengu.oltp.service.common.constant.AppParamConstant;
import cc.rengu.oltp.service.common.dao.SysParamMapper;
import cc.rengu.oltp.service.common.dao.impl.SysParamMapperImpl;
import cc.rengu.oltp.service.common.entity.SysParam;
import cc.rengu.oltp.service.common.constant.TreeNodeConstant;
import cc.rengu.oltp.service.model.BizException;
import cc.rengu.oltp.service.model.BizResponse;
import cc.rengu.oltp.utility.util.DateUtil;
import cc.rengu.oltp.utility.util.RedisUtil;
import cc.rengu.oltp.utility.util.XmlTreeUtil;
import com.alibaba.fastjson.JSON;

import java.util.ArrayList;
import java.util.List;
import java.util.Optional;

/**
 * 银联发起日切开始/结束
 */
public class AcpCupsDateCutService extends ManageService {
    @Override
    protected Object bizBeforeCust() throws Exception {
        return null;
    }

    @Override
    protected BizResponse bizProcessCust(Object request) throws Exception {
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        BizResponse bizResponse = new BizResponse();
        int iRetCode;
        String instId = xmlTreeUtil.getXmlTreeStringValue(TreeNodeConstant.INST_ID);
        String settleDate = xmlTreeUtil.getXmlTreeStringValue(CupsTreeNodeConstant.CUPS_STL_DATE);
        String changeSettleDate = getCupsSettleDate(settleDate);
        rglog.info("cups 清算日期:[{}],转换后日期:[{}]", settleDate, changeSettleDate);
        SysParamMapper sysParamMapper = new SysParamMapperImpl();
        SysParam cupsSettleDate = sysParamMapper.selectSysParamByPrimaryKey(instId, CupsParamConstant.CUPS_PARAM_TYPE, CupsParamConstant.CUPS_SETTLE_DATE);
        if (cupsSettleDate == null) {
            /*插入清算日期*/
            cupsSettleDate = new SysParam();
            cupsSettleDate.setInstId(instId);
            cupsSettleDate.setParamType(CupsParamConstant.CUPS_PARAM_TYPE);
            cupsSettleDate.setParamKey(CupsParamConstant.CUPS_SETTLE_DATE);
            cupsSettleDate.setParamValue(changeSettleDate);
            cupsSettleDate.setCreateTime(DateUtil.getCurrentDateTime("yyyy-MM-dd HH:mm:ss.SSS"));
            cupsSettleDate.setUpdateTime(DateUtil.getCurrentDateTime("yyyy-MM-dd HH:mm:ss.SSS"));
            cupsSettleDate.setParamDesc("cups清算日期");
            iRetCode = sysParamMapper.insertSysParam(cupsSettleDate);
        } else {
            cupsSettleDate.setParamValue(changeSettleDate);
            iRetCode = sysParamMapper.updateSysParamByPrimaryKey(cupsSettleDate);
        }
        if (iRetCode != 0) {
            rglog.error("登记银联清算日期<{}>到参数表失败!", changeSettleDate);
            throw new BizException(RespCodeEnum.SYSTEM_ERROR.getRespCode(), RespCodeEnum.SYSTEM_ERROR.getRespDesc());
        }
        /* 更新银联清算日期到Redis缓存 */
        String paramCacheKey = AppParamConstant.SYS_PARAM_CACHE + ":" + instId.trim() + CupsParamConstant.CUPS_PARAM_TYPE.trim();
        List<SysParam> sysParamList = JSON.parseArray(RedisUtil.onceGet(paramCacheKey), SysParam.class);
        if (null == sysParamList || sysParamList.isEmpty()) {
            sysParamList = new ArrayList<>();
            sysParamList.add(cupsSettleDate);
        } else {
            Optional<SysParam> sysParamOpt = sysParamList.stream().filter(item -> item.getParamKey().trim().equals(CupsParamConstant.CUPS_SETTLE_DATE)).findFirst();
            if (sysParamOpt.isPresent()) {
                sysParamOpt.get().setParamValue(changeSettleDate);
            } else {
                sysParamList.add(cupsSettleDate);
            }
        }
        RedisUtil.onceSet(paramCacheKey, JSON.toJSONString(sysParamList));
        bizResponse.setRspSysId(AppParamConstant.SYS_ID);
        bizResponse.setRespCode(RespCodeEnum.TRANS_SUCCESS.getRespCode());
        bizResponse.setRespDesc(RespCodeEnum.TRANS_SUCCESS.getRespDesc());
        return bizResponse;
    }

    @Override
    protected void bizAfterCust(Object response) throws Exception {

    }

    private String getCupsSettleDate(String oriDate) {
        String dateTime = DateUtil.getCurrentDate();
        String year = dateTime.substring(0, 4);
        String date = dateTime.substring(4, 8);
        if (date.equals(oriDate)) {
            return dateTime;
        } else if ("0101".equals(oriDate) && DateUtil.compareDate(year + oriDate, dateTime) < 0) {
            return String.format("%d", year + 1) + oriDate;
        } else if ("1231".equals(oriDate) && "0101".equals(date)) {
            return String.format("%d", Integer.parseInt(year) - 1) + oriDate;
        }
        return year + oriDate;
    }
}
